﻿@page "/bind-steam/{returnurl}"

@inject NavigationManager NavigationManager
@inject IHttpService _httpService


<CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />


@code {
    [SupplyParameterFromQuery(Name = "openid.claimed_id")]
    [Parameter]
    public string openid_claimed_id { get; set; }

    [Parameter]
    public string returnurl { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(openid_claimed_id))
                {
                    throw new Exception("openid.claimed_id 为空");
                }
                var model = await _httpService.GetAsync<EditUserDataViewModel>("api/space/EditUserData");

                var ids = openid_claimed_id.Split("/");
                if (ids.Length < 2)
                {
                    throw new Exception("openid.claimed_id 格式不正确");
                }

                if (model.SteamIds.Any(s => s == ids.Last()))
                {

                    throw new Exception("SteamId已绑定");

                }

                model.SteamIds.Add(ids.Last());

                var obj = await _httpService.PostAsync<EditUserDataViewModel, Result>("api/space/EditUserData", model);
                //判断结果
                if (obj.Successful == false)
                {
                    throw new Exception(obj.Error);
                }
                else
                {
                    NavigationManager.NavigateTo(ToolHelper.Base64DecodeUrl(returnurl));
                }

            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "绑定Steam失败");
                NavigationManager.NavigateTo(ToolHelper.Base64DecodeUrl(returnurl));
                return;
            }
        }
    }
}
